local shilang = fk.CreateSkill{
  name = "xiaobai__shilang",
}

local XBB = require "packages.xiaobai.util"

Fk:loadTranslationTable{
  ["xiaobai__shilang"] = "逝浪",
  [":xiaobai__shilang"] = "锁定技，一个势力的“小白杯”武将从牌堆中全部消逝后，"..
    "你减1点体力上限，摸2张牌，然后获得一层永久的【酒】效果。",

  ["@xiaobai__shilang"] = "逝浪",
}

--- 重置技能的【酒】效果
--- @param player ServerPlayer @ 玩家
local function refresh_drink(player)
  player.drank = (player.drank or 0) + player:getMark("@xiaobai__shilang")
  player.room:broadcastProperty(player, "drank")
end

shilang:addEffect(fk.AfterCardsMove, {
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(shilang.name) then return end
    local room = player.room
    local move_cards = {}
    for _, move in ipairs(data) do
      if move.toArea == Card.Void then
        for _, info in ipairs(move.moveInfo) do
          table.insertIfNeed(move_cards, info.cardId)
        end
      end
    end
    move_cards = room.logic:moveCardsHoldingAreaCheck(move_cards)
    move_cards = table.filter(move_cards, function (id)
      return XBB.startsWithEndsWith(Fk:getCardById(id).name)
    end)
    local mark_kingdoms = {} ---@type string[]
    local general_marks = player:getTableMark("@&xiaobai__changhe") ---@type string[]
    for _, mark in ipairs(general_marks) do
      table.insertIfNeed(mark_kingdoms, Fk.generals[mark].kingdom)
      if Fk.generals[mark].subkingdom then
        table.insertIfNeed(mark_kingdoms, Fk.generals[mark].subkingdom)
      end
    end

    local move_kingdoms = {} ---@type string[]
    for _, id in ipairs(move_cards) do
      local card = string.gsub(Fk:getCardById(id).name, "_card$", "") ---@type string
      table.insertIfNeed(move_kingdoms, Fk.generals[card].kingdom)
      if Fk.generals[card].subkingdom then
        table.insertIfNeed(move_kingdoms, Fk.generals[card].subkingdom)
      end
    end

    for _, k in ipairs(move_kingdoms) do
      if not table.contains(mark_kingdoms, k) then
        return true
      end
    end
  end,
  trigger_times = function (self, event, target, player, data)
    local room = player.room
    local move_cards = {}
    for _, move in ipairs(data) do
      if move.toArea == Card.Void then
        for _, info in ipairs(move.moveInfo) do
          table.insertIfNeed(move_cards, info.cardId)
        end
      end
    end
    move_cards = room.logic:moveCardsHoldingAreaCheck(move_cards)
    move_cards = table.filter(move_cards, function (id)
      return XBB.startsWithEndsWith(Fk:getCardById(id).name)
    end)
    local mark_kingdoms = {} ---@type string[]
    local general_marks = player:getTableMark("@&xiaobai__changhe") ---@type string[]
    for _, mark in ipairs(general_marks) do
      table.insertIfNeed(mark_kingdoms, Fk.generals[mark].kingdom)
      if Fk.generals[mark].subkingdom then
        table.insertIfNeed(mark_kingdoms, Fk.generals[mark].subkingdom)
      end
    end

    local move_kingdoms = {} ---@type string[]
    for _, id in ipairs(move_cards) do
      local card = string.gsub(Fk:getCardById(id).name, "_card$", "") ---@type string
      table.insertIfNeed(move_kingdoms, Fk.generals[card].kingdom)
      if Fk.generals[card].subkingdom then
        table.insertIfNeed(move_kingdoms, Fk.generals[card].subkingdom)
      end
    end

    local repeat_kingdoms = {} ---@type string[]

    for _, k in ipairs(move_kingdoms) do
      if not table.contains(mark_kingdoms, k) then
        table.insertIfNeed(repeat_kingdoms, k)
      end
    end

    return #repeat_kingdoms
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, -1)
    room:drawCards(player, 2, shilang.name)
    room:addPlayerMark(player, "@xiaobai__shilang", 1)
    refresh_drink(player)
  end
})

shilang:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(shilang.name) and target == player and player:getMark("@xiaobai__shilang") ~= 0 and player.drank == 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.drank = (player.drank or 0) + player:getMark("@xiaobai__shilang")
    player.room:broadcastProperty(player, "drank")
  end,
})

shilang:addEffect(fk.EventTurnChanging, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(shilang.name) and player:getMark("@xiaobai__shilang") > 0 and player.drank == 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.drank = (player.drank or 0) + player:getMark("@xiaobai__shilang")
    player.room:broadcastProperty(player, "drank")
  end,
})

return shilang
